﻿using System.Net.Mail;
using System.ComponentModel.Composition; 
using Fisharoo.Interfaces;
using Fisharoo.Common;


namespace Fisharoo.Components
{
    [Export(typeof(IEmail))]
    public class Email : IEmail
    {
        const string TO_EMAIL_ADDRESS = @"website@fisharoo.com";
        const string FROM_EMAIL_ADDRESS = @"website@fisharoo.com";
        [Import]
        private IConfiguration _configuration;
        
        public void SendEmail(string From, string Subject, string Message)
        {
            MailMessage mm = new MailMessage(From,TO_EMAIL_ADDRESS);
            mm.Subject = Subject;
            mm.Body = Message;

            Send(mm);
        }

        public void SendEmail(string To, string CC, string BCC, string Subject, string Message)
        {
            MailMessage mm = new MailMessage(FROM_EMAIL_ADDRESS,To);
            if(!string.IsNullOrEmpty(CC))
                mm.CC.Add(CC);

            if (!string.IsNullOrEmpty(BCC))
                mm.Bcc.Add(BCC);

            mm.Subject = Subject;
            mm.Body = Message;
            mm.IsBodyHtml = true;

            Send(mm);
        }

        public void SendEmail(string[] To, string[] CC, string[] BCC, string Subject, string Message)
        {
            MailMessage mm = new MailMessage();
            foreach (string to in To)
            {
                mm.To.Add(to);   
            }
            foreach (string cc in CC)
            {
                mm.CC.Add(cc);
            }
            foreach (string bcc in BCC)
            {
                mm.Bcc.Add(bcc);
            }
            mm.From = new MailAddress(FROM_EMAIL_ADDRESS);
            mm.Subject = Subject;
            mm.Body = Message;
            mm.IsBodyHtml = true;

            Send(mm);
        }

        public void SendIndividualEmailsPerRecipient(string[] To, string Subject, string Message)
        {
            foreach (string to in To)
            {
                MailMessage mm = new MailMessage(FROM_EMAIL_ADDRESS,to);
                mm.Subject = Subject;
                mm.Body = Message;
                mm.IsBodyHtml = true;

                Send(mm);
            }
        }

        private void Send(MailMessage Message)
        {
            SmtpClient smtp = new SmtpClient();
            //TODO: During developement we will not be sending mails. uncomment
            //when releasing the code
            //smtp.Send(Message);
        }

        //Chapter 3
        public void SendPasswordReminderEmail(string To, string EncryptedPassword, string Username)
        {
            string Message = "Here is the password you requested: " +
                             Cryptography.Decrypt(EncryptedPassword, Username);
            SendEmail(To, "", "", "Password Reminder", Message);
        }

        public void SendEmailAddressVerificationEmail(string Username, string To)
        {
            MEFManager.Compose(this);
            string rootURL = _configuration.GetConfigurationSetting(typeof(string), "RootURL").ToString();
            string encryptedName = Cryptography.Encrypt(Username, "verify");

            string msg = "Please click on the link below or paste it into a browser to verify your email account.<BR><BR>" +
                            "<a href=\"" + rootURL + "Account/VerifyEmail.aspx?a=" +
                            encryptedName + "\">" +
                            rootURL + "Account/VerifyEmail.aspx?a=" +
                            encryptedName + "</a>";

            SendEmail(To, "", "", "Account created! Email verification required.", msg);
        }
    }
}
